package es.aa.android.cartomatic.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import es.aa.android.cartomatic.R;


public class mSimpleList extends RelativeLayout {

    private Context context;
    private ViewGroup header;
    private ViewGroup footer;
    private mListView list;
    private ProgressBar progressBar;

    public mSimpleList(Context context) {
        super(context);
        initView(context);
    }

    public mSimpleList(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.simple_list, this, true);
        header = (ViewGroup) findViewById(R.id.viewHeader);
        footer = (ViewGroup) findViewById(R.id.viewFooter);
        list = (mListView) findViewById(R.id.gridViewSimpleList);
        list.setVisibility(GONE);
        progressBar = new ProgressBar(context);
    }

    public void addHeader(View header) {
        this.header.addView(header);
    }

    public void removeHeader() {
        header.removeAllViews();
    }

    public void setHeaderVisibility(int visibility) {
        header.setVisibility(visibility);
    }

    public View getHeader() {
        return header;
    }

    public void addFooter(View footer) {
        this.footer.addView(footer);
    }

    public void removeFooter() {
        footer.removeAllViews();
    }

    public void removeFooterView(View view) {
        footer.removeView(view);
    }

    public void setFooterVisibility(int visibility) {
        footer.setVisibility(visibility);
    }

    public void setAdapter(BaseAdapter adapter) {
        list.setAdapter(adapter);
    }

    public void setColumns(int potrait, int landscape) {
        list.setNumColumns(potrait, landscape);
    }

    public void setSwypeLoading(boolean loading) {
        if (loading) {
            addFooter(progressBar);
            header.setVisibility(View.GONE);
            list.setVisibility(View.GONE);
        } else {
            removeView(progressBar);
            header.setVisibility(View.VISIBLE);
            list.setVisibility(View.VISIBLE);
        }
    }

    public void setPageLoading(boolean loading) {
        if (loading) {
            addFooter(progressBar);
        } else {
            footer.removeView(progressBar);
        }
    }

    
    public void setChoiceMode(int choiceMode) {
        list.setChoiceMode(choiceMode);
    }
    

    public void setMultiChoiceListener(AbsListView.MultiChoiceModeListener listener) {
        list.setMultiChoiceModeListener(listener);
    }


    public void setExpanded(boolean expanded) {
        list.setExpanded(expanded);
        if (expanded) {
            list.setVisibility(VISIBLE);
            setHeaderVisibilityOnExpand(VISIBLE);            
        } else {
            list.setVisibility(GONE);
            setHeaderVisibilityOnExpand(GONE);
        }
    }
    
    private void setHeaderVisibilityOnExpand(int visibility) {
    	for (int i = 0; i < header.getChildCount(); i++) {
			if (i != 0) {
				header.getChildAt(i).setVisibility(visibility);
			}
		}
    }

    public boolean isExpanded() {
        return list.isExpanded();
    }
    
    public void setListVisibility(int visibility) {
    	list.setVisibility(visibility);
    }
    
    public int getListVisibility() {
        return list.getVisibility();
    }
    
}
